<template>
  <section class="todoapp">
    <!-- 头部部分 -->
    <Header @addList="addList" />

    <!-- 主体部分 -->
    <Body
      :list="list"
      @destroy="destroy"
      @changeStatus="changeStatus"
      :isCheckAll="isCheckAll"
      @change="change"
    />

    <!-- 底部部分 -->
    <Footer
      @clearList="clearList"
      :leftLength="leftLength"
      :isDoneAll="isDoneAll"
    />
  </section>
</template>

<script>
import Header from '@/components/Header.vue'
import Body from '@/components/Body.vue'
import Footer from '@/components/Footer.vue'
export default {
  data () {
    return {
      list: [
        { id: 1, name: '吃饭', isDone: true },
        { id: 2, name: '睡觉', isDone: false },
        { id: 3, name: '打豆豆', isDone: true }
      ]
    }
  },
  components: {
    Header,
    Body,
    Footer
  },
  methods: {
    //删除功能
    destroy (id) {
      this.list = this.list.filter(item => item.id !== id)
    },
    //清除已完成
    clearList () {
      this.list = this.list.filter(item => !item.isDone)
    },
    //添加功能
    addList (value) {
      const obj = { id: +new Date(), name: value, isDone: false }
      // console.log(obj);
      this.list.unshift(obj)
    },
    //下划线
    changeStatus (id) {
      // console.log(id)
      const res = this.list.find(item => item.id === id)
      res.isDone = !res.isDone
    },
    isCheckAll () {
      return this.list.every(item => item.isDone)
    },
    change (flag) {
      this.list.forEach(item => (item.isDone = flag))
    }
  },
  computed: {
    //剩余未完成长度
    leftLength () {
      return this.list.filter(item => !item.isDone).length
    },
    isDoneAll () {
      return this.list.some(item => item.isDone)
    }
  }
}
</script>

<style></style>
